$(function(){
    var d={
        location:null,
        infoId:null,
        processId:null,
        firstPart:$(".firstPart"),
        secondPart:$(".secondPart"),
        main:$(".main"),
        type:$(".type"),
        subtype:$(".subtype"),
        strength:$(".strength"),
        firstTab:$(".subtype .tab"),
        subtypeContent:$(".subtypeContent"),
        strengthContent:$(".strengthContent"),
        process:$(".process"),
        info:$(".info"),
        secondTab:$(".info .tab"),
        thirdTab:$(".strength .tab"),
        infoContent:$(".infoContent"),
        thirdPart:$(".thirdPart"),
        contentLeft:$(".content-left"),

    };
    var type={
        id:null,
        pageNum:null,
        add:function(id){
            $.ajax({
                type:"get",
                url:"/type/getTypes",
                data:{pageNum:this.pageNum},
                success:function(data){
                    for(var i=0;i<data.content.length;i++){
                        if(data.content[i].id==id){
                            var html=`
                                <div class="row">
                                <div class="col-sm-6">
                                <label>灾害名称 :</label>
                            <input type="text" name="name" disabled value="${data.content[i].name}">
                                </div>
                                <div class="col-sm-6 textRight">
                                <button class="edit"><span></span>编辑</button>
                            <button class="save"><span></span>保存</button>
                            <button class="delete"><span></span>删除</button>
                            </div>
                            </div>
                            <div class="row">
                                <div class="col-sm-12">
                                <label class="verticalTop">特征描述 :</label>
                            <textarea name="signalment" rows="2" disabled>${data.content[i].signalment}</textarea>
                                </div>
                                </div>
                                <div class="row">
                                <div class="col-sm-12">
                                <label class="verticalTop">致灾原因 :</label>
                            <textarea name="disasterReason" rows="3" disabled>${data.content[i].disasterReason}</textarea>
                                </div>
                                </div>
                                <input type="hidden" name="id" value="${data.content[i].id}" class="displayNone">
                                `;
                            d.type.html(html);
                        }
                    }
                    subtype.add(id);
                }
            })
        },
        update:function(){
            var data=d.type.serializeObject();
            $.ajax({
                type:"post",
                url:"/type/updateType",
                dataType:"json",
                data:JSON.stringify(data),
                contentType:"application/json;charset=utf-8",
                success:function(data){
                }
            })
        }
    };
    var subtype={
        add:function(id){
            $.ajax({
                type:"get",
                data:{id:id},
                url:"/type/getSubtypeByTypeId",
                success:function(data){
                    if(data.length!=0){
                        var html1="",html2="",html5="";
                        for(var i=0;i<data.length;i++){
                            var obj=data[i];
                            var str=`<div class="displayNone"><ul class="tab">`;
                            var html3="",html4="";
                            html1+=`<li>子类型${i+1}</li>`;
                            html2+=`<form class="displayNone">
								<div class="row">
									<div class="col-sm-6">
										<label>子类名称 :</label>
										<input type="text" name="name" disabled value="${obj.name}">
									</div>
									<div class="col-sm-6 textRight">
										<button class="edit"><span></span>编辑</button>
										<button class="save"><span></span>保存</button>
										<button class="delete"><span></span>删除</button>
									</div>
								</div>
								<div class="row">
									<div class="col-sm-12">
										<label class="verticalTop">特征描述 :</label>
										<textarea name="description" disabled rows="3">${obj.description}</textarea>
									</div>
								</div>
								<input type="hidden" class="displayNone" name="disasterTypeId" value="${type.id}">
								<input type="hidden" class="displalyNone" name="id" value="${obj.id}">

								</form>`;
                            if(obj.disasterSubtypeStrengths.length!==0){
                                for(var j=0;j<obj.disasterSubtypeStrengths.length;j++){
                                    html3+=`<li>强度${j+1}</li>`;
                                    html4+=`<form class="displayNone">
								<div class="row">
									<div class="col-sm-5">
										<label>灾害等级 :</label>
										<input type="text" name="level" disabled value="${obj.disasterSubtypeStrengths[j].level}">
									</div>
									<div class="col-sm-4">
										<label>数值范围 :</label>
										<input type="text" name="scale" disabled value="${obj.disasterSubtypeStrengths[j].scale}">
									</div>
									<div class="col-sm-3 textRight">
										<button class="edit"><span></span>编辑</button>
										<button class="save"><span></span>保存</button>
										<button class="delete"><span></span>删除</button>
									</div>
								</div>
								<div class="row">
									<div class="col-sm-12">
										<label class="verticalTop">等级描述 :</label>
										<textarea name="description" rows="3" disabled>${obj.disasterSubtypeStrengths[j].description}</textarea>
									</div>
								</div>
								<input type="hidden" class="displayNone" name="disasterSubtypeId" value="${obj.id}">
								<input type="hidden" class="displayNone" name="id" value="${obj.disasterSubtypeStrengths[j].id}">
                            </form>`;
                                }

                            }
                            str=str+html3+`</ul>`+`<div class="strengthContent">`+html4+`</div></div>`;
                            html5+=str;
                        }
                        d.firstTab.html(html1);
                        d.subtypeContent.html(html2);
                        d.strength.html(html5);
                        d.firstTab.children().first().addClass("selected");
                        d.subtypeContent.children().first().removeClass("displayNone");
                        d.strength.children().first().removeClass("displayNone");
                        d.strength.find(".tab").each(function(){
                            $(this).children().first().addClass("selected");
                        })
                        d.strength.find(".strengthContent").each(function(){
                            $(this).children().first().removeClass("displayNone");
                        })

                        //点击切换标签
                        d.firstTab.on("click","li",function(){
                            $(this).addClass("selected");
                            $(this).siblings().removeClass("selected");
                            var index=$(this).index();
                            d.subtypeContent.children().eq(index).removeClass("displayNone");
                            d.subtypeContent.children().eq(index).siblings().addClass("displayNone");
                            d.strength.children().eq(index).removeClass("displayNone");
                            d.strength.children().eq(index).siblings().addClass("displayNone");
                        })
                        d.strength.find(".tab").each(function(){
                            $(this).on("click","li",function(){
                                $(this).addClass("selected");
                                $(this).siblings().removeClass("selected");
                                var index=$(this).index();

                                d.strength.find(".strengthContent").each(function(){
                                    $(this).children().eq(index).removeClass("displayNone");
                                })
                                d.strength.find(".strengthContent").each(function(){
                                    $(this).children().eq(index).siblings().addClass("displayNone");
                                })
                            })
                        })

                    }else{
                        d.subtype.addClass("displayNone");
                        d.strength.addClass("displayNone");

                    }

                }
            })
        },
        update:function(dom){
            var data=dom.serializeObject();
            $.ajax({
                type:"post",
                url:"/type/updateSubtype",
                dataType:"json",
                data:JSON.stringify(data),
                contentType:"application/json;charset=utf-8",
                success:function(data){
                }
            })
        }
    };
    var strength={
        update:function(dom){
            var data=dom.serializeObject();
            $.ajax({
                type:"post",
                url:"/type/updateStrength",
                dataType:"json",
                data:JSON.stringify(data),
                contentType:"application/json;charset=utf-8",
                success:function(data){
                }
            })
        }
    };
    var process={
        id:null,
        pageNum:null,
        add:function(id){
            $.ajax({
                type:"get",
                url:"/process/getProcessById",
                data:{id:id},
                success:function(data){
                    var html1=`
                        <div class="row">
							<div class="col-sm-6 relative">
								<label>灾害地点 :</label>
								<input type="text" name="xsAreaName" class="shuju" disabled value="${data.xsAreaName}">
								<div class="selectBoard left280 top0 displayNone">
										<ul></ul>
								</div>
							</div>
							<div class="col-sm-6 textRight">
								<button class="edit"><span></span>编辑</button>
								<button class="save"><span></span>保存</button>
								<button class="delete"><span></span>删除</button>
							</div>
						</div>
						<div class="row">
							<div class="col-sm-5 relative">
								<label>开始时间 :</label>
								<input type="text" name="startTime" class="dateStart" disabled value="${data.startTime}">
								<div class="schedule-box displayNone date1 top30left60"></div>
							</div>
							<div class="col-sm-6 relative">
								<label>结束时间 :</label>
								<input type="text" name="endTime"  class="dateEnd" disabled value="${data.endTime}">
								<div class="schedule-box displayNone date2"></div>
							</div>
						</div>
						<div class="row">
							<div class="col-sm-5">
								<label>受影响人数 :</label>
								<input type="text" name="affectPeople" class="int" disabled value="${data.affectPeople}">
							</div>
							<div class="col-sm-6">
								<label>伤亡人数 :</label>
								<input type="text" name="hurtPeople" class="int" disabled value="${data.hurtPeople}">
							</div>
						</div>
						<div class="row">
							<div class="col-sm-5">
								<label>受影响建筑 :</label>
								<input type="text" name="collapseBuilding" class="int" disabled value="${data.collapseBuilding}">
							</div>
							<div class="col-sm-6">
								<label>死亡人数 :</label>
								<input type="text" name="diePeople" class="int" disabled value="${data.diePeople}">
							</div>
						</div>
						<div class="row">
							<div class="col-sm-5">
								<label>损失建筑 :</label>
								<input name="damageBuilding" type="text" class="int" disabled value="${data.damageBuilding}">
							</div>
							<div class="col-sm-6">
								<label>道路损失 :</label>
								<input type="text" name="damageRoad" class="int" disabled value="${data.damageRoad}">
							</div>

						</div>
						<div class="row">
							<div class="col-sm-5">
								<label>损失农田 :</label>
								<input type="text" name="damageFarm" class="int" disabled value="${data.damageFarm}">
							</div>
							<div class="col-sm-6">
								<label>经济损失 :</label>
								<input type="text" name="moneyLoss" class="int" disabled value="${data.moneyLoss}">
							</div>
						</div>
						<div class="row">
							<div class="col-sm-12">
								<label>影响面积 :</label>
								<input type="text" name="affectArea" class="int" disabled value="${data.affectArea}">
							</div>
						</div>
						<div class="row">
							<div class="col-sm-12">
								<label class="verticalTop">灾害描述 :</label>
								<textarea name="description"  rows="1" disabled>${data.description}</textarea>
							</div>
						</div>
						<div class="row">
							<div class="col-sm-12">
								<label class="verticalTop">损失描述 :</label>
								<textarea name="lossDescription" rows="3" disabled>${data.lossDescription}</textarea>
							</div>
						</div>
						<input type="hidden" class="displayNone" name="id" value="${data.id}">
                    `;
                    var html2="",html3="";
                    if(data.disasterInfos.length>0){
                        for(var i=0;i<data.disasterInfos.length;i++){
                            var obj=data.disasterInfos[i];
                            html2+=`<li>灾情${i+1}</li>`;
                            html3+=`
                            <form class="displayNone">
                            <div class="row">
								<div class="col-sm-5">
									<label>灾害地点 :</label>
									<input type="text" name="xsAreaName" class="shuju" disabled value="${obj.xsAreaName}">
									<div class="selectBoard left280 top0 displayNone">
										<ul></ul>
								    </div>
								</div>
								<div class="col-sm-4">
									<label>灾害名称 :</label>
									<select name="disasterTypeId"><option value="${obj.disasterTypeId}">${obj.disasterTypeName}</option></select>
								</div>
								<div class="col-sm-3 textRight">
									<button class="edit"><span></span>编辑</button>
									<button class="save"><span></span>保存</button>
									<button class="delete"><span></span>删除</button>
								</div>
							</div>
							<div class="row">
								<div class="col-sm-5">
									<label>子类名称 :</label>
									<select name="disasterSubtypeId" disabled><option value="${obj.disasterSubtypeId}">${obj.disasterSubtypeName}</option></select>
								</div>
								<div class="col-sm-4">
									<label>灾害强度 :</label>
									<select name="strengthId" disabled><option value="${obj.strengthId}">${obj.strengthLevel}</option></select>
								</div>
							</div>
							<div class="row">
								<div class="col-sm-5 relative">
									<label>起始时间 :</label>
									<input type="text" name="startTime" class="dateStart1" disabled value="${obj.startTime}">
									<div class="schedule-box displayNone date4 top30left60"></div>
								</div>
								<div class="col-sm-4 relative">
									<label>结束时间 :</label>
									<input type="text" name="endTime" class="dateEnd1" disabled value="${obj.endTime}">
									<div class="schedule-box displayNone date5"></div>
								</div>
							</div>
							<div class="row">
								<div class="col-sm-12">
									<label class="verticalTop">致灾原因 :</label>
									<textarea name="causes" rows="1" name="causes" disabled>${obj.causes}</textarea>
								</div>
							</div>
							<div class="row">
								<div class="col-sm-12">
									<label class="verticalTop">后期影响 :</label>
									<textarea  rows="1" name="laterInfluence" disabled>${obj.laterInfluence}</textarea>
								</div>

							</div>
							<div class="row">
								<div class="col-sm-12">
									<label class="verticalTop">灾情描述 :</label>
									<textarea name="description" rows="3" disabled>${obj.description}</textarea>
								</div>
							</div>
							<input type="hidden" class="displayNone" name="id" value="${obj.id}">
</form>
                        `;
                        }
                        d.secondTab.html(html2);
                        d.infoContent.html(html3);
                    }else{
                        d.info.html("<h3 class='invalid'>尚未录入灾情信息！</h3>");
                    }
                    d.process.html(html1);
                    d.secondTab.children().first().addClass("selected");
                    d.infoContent.children().first().removeClass("displayNone");
                    //点击切换标签
                    d.secondTab.on("click","li",function(){
                        $(this).addClass("selected");
                        $(this).siblings().removeClass("selected");
                        var index=$(this).index();
                        d.infoContent.children().eq(index).removeClass("displayNone");
                        d.infoContent.children().eq(index).siblings().addClass("displayNone");
                    })
                    //乡镇名称
                    $(function(){
                        $.ajax({
                            type:"GET",
                            url:"/process/getAreaName",
                            success:function (data) {
                                var str="";
                                for(var i=0;i<data.length;i++){
                                    str+=`<li><label><input type="checkbox" value="${data[i]}">${data[i]}</label></li>`;
                                }
                                str+=`<li class="textRight w100"><span class="clear">重置</span><span class="sure">确定</span></li>`;
                                $(".selectBoard ul").html(str);
                            }
                        });

                        var arr=[];
                        var selectBoard=$(".selectBoard");

//仿select的点击事件

                        $(".shuju").each(function(){
                            $(this).click(function(){
                                $(this).next().removeClass("displayNone");
                                $(this).val("");
                            })
                        })

//按钮组
                        selectBoard.each(function(){
                            $(this).on("click",".clear",function(){
                                $(this).parent().siblings().each(function(){
                                    $(this).find("input").removeAttr("checked")
                                });
                                arr=[];
                            })
                        })
                        selectBoard.each(function(){
                            $(this).on("click",".sure",function(){
                                //显示值
                                $(this).parents(".selectBoard").prev().val(arr.join(" "));
                                //清空
                                $(this).parent().siblings().each(function(){
                                    $(this).find("input").removeAttr("checked")
                                });
                                arr=[];
                                //关闭选择页面
                                $(this).parents(".selectBoard").addClass("displayNone");
                            })
                        })

//监听checkbox的value值 改变则执行下列操作
                        selectBoard.each(function(){
                            $(this).on("change","input",function(){
                                if ($(this).prop("checked")) {
                                    arr.push($(this).val());
                                }else{
                                    removeArrVal(arr,$(this).val())
                                }
                                console.log(arr);
                            });
                        })



                        function removeArrVal(arr, val) {
                            for(var i=0; i<arr.length; i++) {
                                if(arr[i]===val) {
                                    arr.splice(i, 1);
                                    break;
                                }
                            }
                        }


                        d.process.children().not(".row:first-child").click(function(){
                            selectBoard.addClass("displayNone");
                        })
                        $(".infoContent form").children().not(".row:first-child").click(function(){
                            selectBoard.addClass("displayNone");
                        })

                    });
                    //时间
                    (function () {

                        $(".dateStart,.dateEnd,.dateStart1,.dateEnd1").focus(function(){
                            var that=$(this);
                            that.next().removeClass("displayNone");
                            that.parent().siblings().click(function(){
                                that.next().addClass("displayNone");
                            })
                            that.parent().parent().siblings().click(function(){
                                that.next().addClass("displayNone");
                            })
                        });
                        var _global;
                        //工具函数
                        //配置合并
                        function extend (def,opt,override) {
                            for(var k in opt){
                                if(opt.hasOwnProperty(k) && (!def.hasOwnProperty(k) || override)){
                                    def[k] = opt[k]
                                }
                            }
                            return def;
                        }
                        //日期格式化
                        function formartDate (y,m,d,symbol) {
                            symbol = symbol || '-';
                            m = (m.toString())[1] ? m : '0'+m;
                            d = (d.toString())[1] ? d : '0'+d;
                            return y+symbol+m+symbol+d
                        }

                        function Schedule (opt) {
                            var def = {},
                                opt = extend(def,opt,true),
                                curDate = opt.date ? new Date(opt.date) : new Date(),
                                year = curDate.getFullYear(),
                                month = curDate.getMonth(),
                                day = curDate.getDate(),
                                currentYear = curDate.getFullYear(),
                                currentMonth = curDate.getMonth(),
                                currentDay = curDate.getDate(),
                                selectedDate = '',
                                el = document.querySelector(opt.el),
                                _this = this;
                            var bindEvent = function (){
                                el.addEventListener('click',function(e){
                                    switch (e.target.id) {
                                        case 'nextMonth':
                                            _this.nextMonthFun();
                                            break;
                                        case 'nextYear':
                                            _this.nextYearFun();
                                            break;
                                        case 'prevMonth':
                                            _this.prevMonthFun();
                                            break;
                                        case 'prevYear':
                                            _this.prevYearFun();
                                            break;
                                        default:
                                            break;
                                    };
                                    if(e.target.className.indexOf('currentDate') > -1){
                                        opt.clickCb && opt.clickCb(year, month+1, e.target.innerHTML);
                                        selectedDate = e.target.title;
                                        day = e.target.innerHTML;
                                        render();
                                    }
                                },false)
                            }
                            var init = function () {
                                var scheduleHd = '<div class="schedule-hd">'+
                                    '<div>'+
                                    '<span class="arrow" id="prevYear" >&#8606</span>'+
                                    '<span class="arrow " id="prevMonth">&#8592</span>'+
                                    '</div>'+
                                    '<div class="today">'+formartDate(year,month+1,day,'-')+'</div>'+
                                    '<div>'+
                                    '<span class="arrow" id="nextMonth">&#8594</span>'+
                                    '<span class="arrow" id="nextYear">&#8608</span>'+
                                    '</div>'+
                                    '</div>'
                                var scheduleWeek = '<ul class="week-ul ul-box">'+
                                    '<li>日</li>'+
                                    '<li>一</li>'+
                                    '<li>二</li>'+
                                    '<li>三</li>'+
                                    '<li>四</li>'+
                                    '<li>五</li>'+
                                    '<li>六</li>'+
                                    '</ul>'
                                var scheduleBd = '<ul class="schedule-bd ul-box" ></ul>';
                                el.innerHTML = scheduleHd + scheduleWeek + scheduleBd;
                                bindEvent();
                                render();
                            }
                            var render = function () {
                                var fullDay = new Date(year,month+1,0).getDate(), //当月总天数
                                    startWeek = new Date(year,month,1).getDay(), //当月第一天是周几
                                    total = (fullDay+startWeek)%7 == 0 ? (fullDay+startWeek) : fullDay+startWeek+(7-(fullDay+startWeek)%7),//元素总个数
                                    lastMonthDay = new Date(year,month,0).getDate(), //上月最后一天
                                    eleTemp = [];
                                for(var i = 0; i < total; i++){
                                    if(i<startWeek){
                                        eleTemp.push('<li class="other-month"><span class="dayStyle">'+(lastMonthDay-startWeek+1+i)+'</span></li>')
                                    }else if(i<(startWeek+fullDay)){
                                        var nowDate = formartDate(year,month+1,(i+1-startWeek),'-');
                                        var addClass = '';
                                        selectedDate == nowDate && (addClass = 'selected-style');
                                        formartDate(currentYear,currentMonth+1,currentDay,'-') == nowDate && (addClass = 'today-flag');
                                        eleTemp.push('<li class="current-month" ><span title='+nowDate+' class="currentDate dayStyle '+addClass+'">'+(i+1-startWeek)+'</span></li>')
                                    }else{
                                        eleTemp.push('<li class="other-month"><span class="dayStyle">'+(i+1-(startWeek+fullDay))+'</span></li>')
                                    }
                                }
                                el.querySelector('.schedule-bd').innerHTML = eleTemp.join('');
                                el.querySelector('.today').innerHTML = formartDate(year,month+1,day,'-');
                            };
                            this.nextMonthFun = function () {
                                if(month+1 > 11){
                                    year += 1;
                                    month = 0;
                                }else{
                                    month += 1;
                                }
                                render();
                                opt.nextMonthCb && opt.nextMonthCb(year,month+1,day);
                            },
                                this.nextYearFun = function () {
                                    year += 1;
                                    render();
                                    opt.nextYeayCb && opt.nextYeayCb(year,month+1,day);
                                },
                                this.prevMonthFun = function () {
                                    if(month-1 < 0){
                                        year -= 1;
                                        month = 11;
                                    }else{
                                        month -= 1;
                                    }
                                    render();
                                    opt.prevMonthCb && opt.prevMonthCb(year,month+1,day);
                                },
                                this.prevYearFun = function () {
                                    year -= 1;
                                    render();
                                    opt.prevYearCb && opt.prevYearCb(year,month+1,day);
                                }
                            init();
                        }
                        //将插件暴露给全局对象
                        _global = (function(){return this || (0,eval)('this')}());
                        if(typeof module !== 'undefined' && module.exports){
                            module.exports = Schedule;
                        }else if (typeof define === "function" && define.amd){
                            define(function () {
                                return Schedule;
                            })
                        }else {
                            !('Schedule' in _global) && (_global.Schedule = Schedule);
                        }

                        var mySchedule1 = new Schedule({
                            el: '.date1',
                            //date: '2018-9-20',
                            clickCb: function (y,m,d) {
                                document.querySelector('.dateStart').value = y+'-'+m+'-'+d;
                            },
                            nextMonthCb: function (y,m,d) {
                                document.querySelector('.dateStart').value = y+'-'+m+'-'+d;
                            },
                            nextYeayCb: function (y,m,d) {
                                document.querySelector('.dateStart').value = y+'-'+m+'-'+d;
                            },
                            prevMonthCb: function (y,m,d) {
                                document.querySelector('.dateStart').value = y+'-'+m+'-'+d;
                            },
                            prevYearCb: function (y,m,d) {
                                document.querySelector('.dateStart').value = y+'-'+m+'-'+d;
                            }
                        });
                        var mySchedule2 = new Schedule({
                            el: '.date2',
                            //date: '2018-9-20',
                            clickCb: function (y,m,d) {
                                document.querySelector('.dateEnd').value = y+'-'+m+'-'+d;
                            },
                            nextMonthCb: function (y,m,d) {
                                document.querySelector('.dateEnd').value = y+'-'+m+'-'+d;
                            },
                            nextYeayCb: function (y,m,d) {
                                document.querySelector('.dateEnd').value = y+'-'+m+'-'+d;
                            },
                            prevMonthCb: function (y,m,d) {
                                document.querySelector('.dateEnd').value = y+'-'+m+'-'+d;
                            },
                            prevYearCb: function (y,m,d) {
                                document.querySelector('.dateEnd').value = y+'-'+m+'-'+d;
                            }
                        });
                        var mySchedule4 = new Schedule({
                            el: '.date4',
                            //date: '2018-9-20',
                            clickCb: function (y,m,d) {
                                document.querySelector('.dateStart1').value = y+'-'+m+'-'+d;
                            },
                            nextMonthCb: function (y,m,d) {
                                document.querySelector('.dateStart1').value = y+'-'+m+'-'+d;
                            },
                            nextYeayCb: function (y,m,d) {
                                document.querySelector('.dateStart1').value = y+'-'+m+'-'+d;
                            },
                            prevMonthCb: function (y,m,d) {
                                document.querySelector('.dateStart1').value = y+'-'+m+'-'+d;
                            },
                            prevYearCb: function (y,m,d) {
                                document.querySelector('.dateStart1').value = y+'-'+m+'-'+d;
                            }
                        });
                        var mySchedule5 = new Schedule({
                            el: '.date5',
                            //date: '2018-9-20',
                            clickCb: function (y,m,d) {
                                document.querySelector('.dateEnd1').value = y+'-'+m+'-'+d;
                            },
                            nextMonthCb: function (y,m,d) {
                                document.querySelector('.dateEnd1').value = y+'-'+m+'-'+d;
                            },
                            nextYeayCb: function (y,m,d) {
                                document.querySelector('.dateEnd1').value = y+'-'+m+'-'+d;
                            },
                            prevMonthCb: function (y,m,d) {
                                document.querySelector('.dateEnd1').value = y+'-'+m+'-'+d;
                            },
                            prevYearCb: function (y,m,d) {
                                document.querySelector('.dateEnd1').value = y+'-'+m+'-'+d;
                            }
                        });


                    }());
                }
            })
        },
        update:function(dom){
            var data=dom.serializeObject();
            $.ajax({
                type:"post",
                url:"/process/updateProcess",
                dataType:"json",
                data:JSON.stringify(data),
                contentType:"application/json;charset=utf-8",
                success:function(data){
                }
            })
        }
    };
    var info={
        id:null,
        pageNum:null,
        add:function(id){
            $.ajax({
                type:"get",
                url:"/process/getInfoById",
                data:{id:id},
                success:function(data){
                    var html=`
                         <form>
                            <div class="row">
								<div class="col-sm-5">
									<label>灾害地点 :</label>
									<input type="text" name="xsAreaName" class="shuju" disabled value="${data.xsAreaName}">
									<div class="selectBoard left280 top0 displayNone">
										<ul></ul>
								    </div>
								</div>
								<div class="col-sm-4">
									<label>灾害名称 :</label>
									<select name="disasterTypeId" class="disasterTypeId"><option value="${data.disasterTypeId}"></option></select>
								</div>
								<div class="col-sm-3 textRight">
									<button class="edit"><span></span>编辑</button>
									<button class="save"><span></span>保存</button>
									<button class="delete"><span></span>删除</button>
								</div>
							</div>
							<div class="row">
								<div class="col-sm-5">
									<label>子类名称 :</label>
									<select name="disasterSubtypeId" class="disasterSubtypeId" disabled><option value="${data.disasterSubtypeId}"></option></select>
								</div>
								<div class="col-sm-4">
									<label>灾害强度 :</label>
									<select name="strengthId" disabled class="strengthId"><option value="${data.strengthId}"></option></select>
								</div>
							</div>
							<div class="row">
								<div class="col-sm-5 relative">
									<label>起始时间 :</label>
									<input type="text" name="startTime" class="dateStart" disabled value="${data.startTime}">
									<div class="schedule-box displayNone date1 top30left60"></div>
								</div>
								<div class="col-sm-4 relative">
									<label>结束时间 :</label>
									<input type="text" name="endTime" class="dateEnd" disabled value="${data.endTime}">
									<div class="schedule-box displayNone date2 top30left60"></div>
								</div>
							</div>
							<div class="row">
								<div class="col-sm-12">
									<label class="verticalTop">致灾原因 :</label>
									<textarea name="causes" rows="1" name="causes" disabled>${data.causes}</textarea>
								</div>

							</div>
							<div class="row">
								<div class="col-sm-12">
									<label class="verticalTop">后期影响 :</label>
									<textarea  rows="1" name="laterInfluence" disabled>${data.laterInfluence}</textarea>
								</div>

							</div>
							<div class="row">
								<div class="col-sm-12">
									<label class="verticalTop">灾情描述 :</label>
									<textarea name="description" rows="3" disabled>${data.description}</textarea>
								</div>
							</div>
							<input type="hidden" class="displayNone" name="id" value="${data.id}">
</form>
                    `;
                    d.thirdPart.html(html);
                    var disasterTypeId=$(".disasterTypeId").val();
                    var disasterSubtypeId=$(".disasterSubtypeId").val();
                    var strengthId=$(".strengthId").val();
                    $.ajax({
                        type:"get",
                        url:"/widget/linkageTypeName",
                        success:function(data){
                            for(var i=0;i<data.length;i++){
                                if(data[i].id==disasterTypeId){
                                    $(".disasterTypeId").find("option").text(data[i].content)
                                }
                            }
                        }
                    });
                    $.ajax({
                        type:"get",
                        url:"/widget/linkageSubtypeName",
                        data:{id:disasterTypeId},
                        success:function(data){
                            for(var i=0;i<data.length;i++) {
                                if (data[i].id == disasterSubtypeId) {
                                    $(".disasterSubtypeId").find("option").text(data[i].content)
                                }
                            }

                        }
                    });
                    $.ajax({
                        type:"get",
                        url:"/widget/linkageStrengthLevel",
                        data:{id:disasterSubtypeId},
                        success:function(data){
                            for(var i=0;i<data.length;i++) {
                                if (data[i].id == strengthId) {
                                    $(".strengthId").find("option").text(data[i].content)
                                }
                            }

                        }
                    });

                    //乡镇名称
                    $(function(){
                        $.ajax({
                            type:"GET",
                            url:"/process/getAreaName",
                            success:function (data) {
                                var str="";
                                for(var i=0;i<data.length;i++){
                                    str+=`<li><label><input type="checkbox" value="${data[i]}">${data[i]}</label></li>`;
                                }
                                str+=`<li class="textRight w100"><span class="clear">重置</span><span class="sure">确定</span></li>`;
                                $(".selectBoard ul").html(str);
                            }
                        });

                        var arr=[];
                        var selectBoard=$(".selectBoard");

//仿select的点击事件

                        $(".shuju").each(function(){
                            $(this).click(function(){
                                $(this).next().removeClass("displayNone");
                                $(this).val("");
                            })
                        })

//按钮组
                        selectBoard.each(function(){
                            $(this).on("click",".clear",function(){
                                $(this).parent().siblings().each(function(){
                                    $(this).find("input").removeAttr("checked")
                                });
                                arr=[];
                            })
                        })
                        selectBoard.each(function(){
                            $(this).on("click",".sure",function(){
                                //显示值
                                $(this).parents(".selectBoard").prev().val(arr.join(" "));
                                //清空
                                $(this).parent().siblings().each(function(){
                                    $(this).find("input").removeAttr("checked")
                                });
                                arr=[];
                                //关闭选择页面
                                $(this).parents(".selectBoard").addClass("displayNone");
                            })
                        })

//监听checkbox的value值 改变则执行下列操作
                        selectBoard.each(function(){
                            $(this).on("change","input",function(){
                                if ($(this).prop("checked")) {
                                    arr.push($(this).val());
                                }else{
                                    removeArrVal(arr,$(this).val())
                                }
                                console.log(arr);
                            });
                        })



                        function removeArrVal(arr, val) {
                            for(var i=0; i<arr.length; i++) {
                                if(arr[i]===val) {
                                    arr.splice(i, 1);
                                    break;
                                }
                            }
                        }


                        $(".thirdPart form").children().not(".row:first-child").click(function(){
                            selectBoard.addClass("displayNone");
                        })

                    });
                    //时间
                    (function(){

                        $(".dateStart,.dateEnd,.dateStart1,.dateEnd1").focus(function(){
                            var that=$(this);
                            that.next().removeClass("displayNone");
                            that.parent().siblings().click(function(){
                                that.next().addClass("displayNone");
                            })
                            that.parent().parent().siblings().click(function(){
                                that.next().addClass("displayNone");
                            })
                        });
                        var _global;
                        //工具函数
                        //配置合并
                        function extend (def,opt,override) {
                            for(var k in opt){
                                if(opt.hasOwnProperty(k) && (!def.hasOwnProperty(k) || override)){
                                    def[k] = opt[k]
                                }
                            }
                            return def;
                        }
                        //日期格式化
                        function formartDate (y,m,d,symbol) {
                            symbol = symbol || '-';
                            m = (m.toString())[1] ? m : '0'+m;
                            d = (d.toString())[1] ? d : '0'+d;
                            return y+symbol+m+symbol+d
                        }

                        function Schedule (opt) {
                            var def = {},
                                opt = extend(def,opt,true),
                                curDate = opt.date ? new Date(opt.date) : new Date(),
                                year = curDate.getFullYear(),
                                month = curDate.getMonth(),
                                day = curDate.getDate(),
                                currentYear = curDate.getFullYear(),
                                currentMonth = curDate.getMonth(),
                                currentDay = curDate.getDate(),
                                selectedDate = '',
                                el = document.querySelector(opt.el),
                                _this = this;
                            var bindEvent = function (){
                                el.addEventListener('click',function(e){
                                    switch (e.target.id) {
                                        case 'nextMonth':
                                            _this.nextMonthFun();
                                            break;
                                        case 'nextYear':
                                            _this.nextYearFun();
                                            break;
                                        case 'prevMonth':
                                            _this.prevMonthFun();
                                            break;
                                        case 'prevYear':
                                            _this.prevYearFun();
                                            break;
                                        default:
                                            break;
                                    };
                                    if(e.target.className.indexOf('currentDate') > -1){
                                        opt.clickCb && opt.clickCb(year, month+1, e.target.innerHTML);
                                        selectedDate = e.target.title;
                                        day = e.target.innerHTML;
                                        render();
                                    }
                                },false)
                            }
                            var init = function () {
                                var scheduleHd = '<div class="schedule-hd">'+
                                    '<div>'+
                                    '<span class="arrow" id="prevYear" >&#8606</span>'+
                                    '<span class="arrow " id="prevMonth">&#8592</span>'+
                                    '</div>'+
                                    '<div class="today">'+formartDate(year,month+1,day,'-')+'</div>'+
                                    '<div>'+
                                    '<span class="arrow" id="nextMonth">&#8594</span>'+
                                    '<span class="arrow" id="nextYear">&#8608</span>'+
                                    '</div>'+
                                    '</div>'
                                var scheduleWeek = '<ul class="week-ul ul-box">'+
                                    '<li>日</li>'+
                                    '<li>一</li>'+
                                    '<li>二</li>'+
                                    '<li>三</li>'+
                                    '<li>四</li>'+
                                    '<li>五</li>'+
                                    '<li>六</li>'+
                                    '</ul>'
                                var scheduleBd = '<ul class="schedule-bd ul-box" ></ul>';
                                el.innerHTML = scheduleHd + scheduleWeek + scheduleBd;
                                bindEvent();
                                render();
                            }
                            var render = function () {
                                var fullDay = new Date(year,month+1,0).getDate(), //当月总天数
                                    startWeek = new Date(year,month,1).getDay(), //当月第一天是周几
                                    total = (fullDay+startWeek)%7 == 0 ? (fullDay+startWeek) : fullDay+startWeek+(7-(fullDay+startWeek)%7),//元素总个数
                                    lastMonthDay = new Date(year,month,0).getDate(), //上月最后一天
                                    eleTemp = [];
                                for(var i = 0; i < total; i++){
                                    if(i<startWeek){
                                        eleTemp.push('<li class="other-month"><span class="dayStyle">'+(lastMonthDay-startWeek+1+i)+'</span></li>')
                                    }else if(i<(startWeek+fullDay)){
                                        var nowDate = formartDate(year,month+1,(i+1-startWeek),'-');
                                        var addClass = '';
                                        selectedDate == nowDate && (addClass = 'selected-style');
                                        formartDate(currentYear,currentMonth+1,currentDay,'-') == nowDate && (addClass = 'today-flag');
                                        eleTemp.push('<li class="current-month" ><span title='+nowDate+' class="currentDate dayStyle '+addClass+'">'+(i+1-startWeek)+'</span></li>')
                                    }else{
                                        eleTemp.push('<li class="other-month"><span class="dayStyle">'+(i+1-(startWeek+fullDay))+'</span></li>')
                                    }
                                }
                                el.querySelector('.schedule-bd').innerHTML = eleTemp.join('');
                                el.querySelector('.today').innerHTML = formartDate(year,month+1,day,'-');
                            };
                            this.nextMonthFun = function () {
                                if(month+1 > 11){
                                    year += 1;
                                    month = 0;
                                }else{
                                    month += 1;
                                }
                                render();
                                opt.nextMonthCb && opt.nextMonthCb(year,month+1,day);
                            },
                                this.nextYearFun = function () {
                                    year += 1;
                                    render();
                                    opt.nextYeayCb && opt.nextYeayCb(year,month+1,day);
                                },
                                this.prevMonthFun = function () {
                                    if(month-1 < 0){
                                        year -= 1;
                                        month = 11;
                                    }else{
                                        month -= 1;
                                    }
                                    render();
                                    opt.prevMonthCb && opt.prevMonthCb(year,month+1,day);
                                },
                                this.prevYearFun = function () {
                                    year -= 1;
                                    render();
                                    opt.prevYearCb && opt.prevYearCb(year,month+1,day);
                                }
                            init();
                        }
                        //将插件暴露给全局对象
                        _global = (function(){return this || (0,eval)('this')}());
                        if(typeof module !== 'undefined' && module.exports){
                            module.exports = Schedule;
                        }else if (typeof define === "function" && define.amd){
                            define(function () {
                                return Schedule;
                            })
                        }else {
                            !('Schedule' in _global) && (_global.Schedule = Schedule);
                        }

                        var mySchedule1 = new Schedule({
                            el: '.date1',
                            //date: '2018-9-20',
                            clickCb: function (y,m,d) {
                                document.querySelector('.dateStart').value = y+'-'+m+'-'+d;
                            },
                            nextMonthCb: function (y,m,d) {
                                document.querySelector('.dateStart').value = y+'-'+m+'-'+d;
                            },
                            nextYeayCb: function (y,m,d) {
                                document.querySelector('.dateStart').value = y+'-'+m+'-'+d;
                            },
                            prevMonthCb: function (y,m,d) {
                                document.querySelector('.dateStart').value = y+'-'+m+'-'+d;
                            },
                            prevYearCb: function (y,m,d) {
                                document.querySelector('.dateStart').value = y+'-'+m+'-'+d;
                            }
                        });
                        var mySchedule2 = new Schedule({
                            el: '.date2',
                            //date: '2018-9-20',
                            clickCb: function (y,m,d) {
                                document.querySelector('.dateEnd').value = y+'-'+m+'-'+d;
                            },
                            nextMonthCb: function (y,m,d) {
                                document.querySelector('.dateEnd').value = y+'-'+m+'-'+d;
                            },
                            nextYeayCb: function (y,m,d) {
                                document.querySelector('.dateEnd').value = y+'-'+m+'-'+d;
                            },
                            prevMonthCb: function (y,m,d) {
                                document.querySelector('.dateEnd').value = y+'-'+m+'-'+d;
                            },
                            prevYearCb: function (y,m,d) {
                                document.querySelector('.dateEnd').value = y+'-'+m+'-'+d;
                            }
                        });
                        var mySchedule4 = new Schedule({
                            el: '.date4',
                            //date: '2018-9-20',
                            clickCb: function (y,m,d) {
                                document.querySelector('.dateStart1').value = y+'-'+m+'-'+d;
                            },
                            nextMonthCb: function (y,m,d) {
                                document.querySelector('.dateStart1').value = y+'-'+m+'-'+d;
                            },
                            nextYeayCb: function (y,m,d) {
                                document.querySelector('.dateStart1').value = y+'-'+m+'-'+d;
                            },
                            prevMonthCb: function (y,m,d) {
                                document.querySelector('.dateStart1').value = y+'-'+m+'-'+d;
                            },
                            prevYearCb: function (y,m,d) {
                                document.querySelector('.dateStart1').value = y+'-'+m+'-'+d;
                            }
                        });
                        var mySchedule5 = new Schedule({
                            el: '.date5',
                            //date: '2018-9-20',
                            clickCb: function (y,m,d) {
                                document.querySelector('.dateEnd1').value = y+'-'+m+'-'+d;
                            },
                            nextMonthCb: function (y,m,d) {
                                document.querySelector('.dateEnd1').value = y+'-'+m+'-'+d;
                            },
                            nextYeayCb: function (y,m,d) {
                                document.querySelector('.dateEnd1').value = y+'-'+m+'-'+d;
                            },
                            prevMonthCb: function (y,m,d) {
                                document.querySelector('.dateEnd1').value = y+'-'+m+'-'+d;
                            },
                            prevYearCb: function (y,m,d) {
                                document.querySelector('.dateEnd1').value = y+'-'+m+'-'+d;
                            }
                        });


                    }());
                }
            })
        },
        update:function(dom){
            var data=dom.serializeObject();
            $.ajax({
                type:"post",
                url:"/process/updateInfo",
                dataType:"json",
                data:JSON.stringify(data),
                contentType:"application/json;charset=utf-8",
                success:function(data){
                }
            })
        }
    };


    d.location=c.locationHref();

    if(d.location[0]==="typeId") {
        type.id=d.location[1];
        type.pageNum=d.location[3];
        d.firstPart.removeClass("displayNone");
        type.add(type.id);
    }
    else if(d.location[0]==="processId"){
        process.id=d.location[1];
        process.pageNum=d.location[3];
        d.secondPart.removeClass("displayNone");
        d.contentLeft.find("li:nth-child(2)").addClass("selected");
        d.contentLeft.find("li:nth-child(2)").siblings().removeClass("selected");
        process.add(process.id);
    }
    else if(d.location[0]==="infoId"){
        info.id=d.location[1];
        info.pageNum=d.location[3];
        d.thirdPart.removeClass("displayNone");
        d.contentLeft.find("li:nth-child(3)").addClass("selected");
        d.contentLeft.find("li:nth-child(3)").siblings().removeClass("selected");
        info.add(info.id)
    }

    //按钮组
    d.main.on("click",".edit",function(e){
        e.preventDefault();
        $(this).parent().parent().parent().find("input,textarea,select").addClass("borderColor").removeAttr("disabled");
    });
    $.fn.serializeObject = function(){
        var o = {};
        var a = this.serializeArray();
        $.each(a, function() {
            if (o[this.name ]){
                if (!o[this.name ].push) {
                    o[this.name ] = [o[this.name ]];
                }
                o[this.name ].push(this.value || '');
            }else {
                o[this.name ] = this.value || '';
            }
        });
        return o;
    };
    d.main.on("click",".type .save",function(e){
        e.preventDefault();
        if(isEmpty(d.type)===false){return false}
        type.update();
        d.type.find("input,textarea").removeAttr("placeholder").removeClass("borderColor").attr("disabled","disabled");
    });
    d.main.on("click",".type .delete",function(e){
        e.preventDefault();
        var id=$(this).parent().parent().parent().children().last().val();
        $.ajax({
            type:"get",
            url:"/type/deleteType",
            data:{id:id},
            success:function(data){
                console.log("删除成功")
            }
        })
        //页面
        d.type.html("");
        d.firstTab.html("");
        d.subtypeContent.html("");
        d.strengthContent.html("");
        d.firstPart.html("<h3 class='invalid'>已删除！</h3>")
        //删除数据
    });
    d.main.on("click",".subtype .save",function(e){
        e.preventDefault();
        if(isEmpty($(".subtype form"))===false){return false}
        subtype.update($(this).parent().parent().parent());
        $(".subtype form").find("input,textarea").removeAttr("placeholder").removeClass("borderColor").attr("disabled","disabled");
    });
    d.main.on("click",".subtype .delete",function(e){
        e.preventDefault();
        //数据库
        //数据库
        var id=$(this).parent().parent().parent().children().last().val();
        $.ajax({
            type:"get",
            url:"/type/deleteSubtype",
            data:{id:id},
            success:function(data){
                console.log("删除成功")
            }
        })
        window.location.reload(true);
        //页面
        $(this).parent().parent().parent().html("");
    });
    d.main.on("click",".strength .save",function(e){
        e.preventDefault();
        if(isEmpty($(".strength form"))===false){return false}
        if(isInt($(".strength form"))===false){return false}
        strength.update($(this).parent().parent().parent());
        $(".strength form").find("input,textarea").removeAttr("placeholder").removeClass("borderColor").attr("disabled","disabled");
    });
    d.main.on("click",".strength .delete",function(e){
        e.preventDefault();
        //数据库
        var id=$(this).parent().parent().parent().children().last().val();
        $.ajax({
            type:"get",
            url:"/type/deleteStrength",
            data:{id:id},
            success:function(data){
                console.log("删除成功")
            }
        })
        //页面
        $(this).parent().parent().parent().html("");
    });

    d.main.on("click",".process .save",function(e){
        e.preventDefault();
        if(isEmpty(d.process)===false){return false}
        if(isInt(d.process)===false){return false}
        if(compare(d.process.find(".dateStart"),d.process.find(".dateEnd"))===false){return false}
        process.update(d.process);
        d.process.find("input,textarea").removeAttr("placeholder").removeClass("borderColor").attr("disabled","disabled");
    });
    d.main.on("click",".process .delete",function(e){
        e.preventDefault();
        var id=$(this).parent().parent().parent().children().last().val();
        $.ajax({
            type:"get",
            url:"/process/deleteProcess",
            data:{id:id},
            success:function(data){
                console.log("删除成功")
            }
        })
        //页面
        d.process.html("");
        d.secondTab.html("");
        d.infoContent.html("");
    });
    d.main.on("click",".info .save",function(e){
        e.preventDefault();
        if(isEmpty(d.info.find("form"))===false){return false}
        if(isInt(d.info.find("form"))===false){return false}
        if(compare(d.info.find(".dateStart1"),d.info.find(".dateEnd1"))===false){return false}
        info.update($(this).parent().parent().parent());
        d.info.find("input,textarea,select").removeAttr("placeholder").removeClass("borderColor").attr("disabled","disabled");

    });
    d.main.on("click",".info .delete",function(e){
        e.preventDefault();
        //数据库
        var id=$(this).parent().parent().parent().children().last().val();
        $.ajax({
            type:"get",
            url:"/process/deleteInfo",
            data:{id:id},
            success:function(data){
                console.log("删除成功")
            }
        })
        //页面
        $(this).parent().parent().parent().html("");
    });

    d.main.on("click",".thirdPart .save",function(e){
        e.preventDefault();
        if(isEmpty(d.thirdPart.find("form"))===false){return false}
        if(isInt(d.thirdPart.find("form"))===false){return false}
        if(compare(d.thirdPart.find(".dateStart"),d.thirdPart.find(".dateEnd"))===false){return false}
        info.update($(this).parent().parent().parent());
        d.thirdPart.find("input,textarea,select").removeAttr("placeholder").removeClass("borderColor").attr("disabled","disabled");
    });
    d.main.on("click",".info .delete",function(e){
        e.preventDefault();
        //数据库
        var id=$(this).parent().parent().parent().children().last().val();
        $.ajax({
            type:"get",
            url:"/process/deleteInfo",
            data:{id:id},
            success:function(data){
                console.log("删除成功")
            }
        })
        //页面
        $(this).parent().parent().parent().html("");
    });







    //返回上一页
    $(".back").click(function(){
        if(d.location[0]==="typeId"){
            location.href=`manage.html?xxxId=0&pageNum=${type.pageNum}`;
        }
        if(d.location[0]==="processId"){
            location.href=`manage.html?xxxId=1&pageNum=${process.pageNum}`;
        }
        if(d.location[0]==="infoId"){
            location.href=`manage.html?xxxId=2&pageNum=${info.pageNum}`;
        }
    });

    d.contentLeft.find("li").click(function(){
        location.href=`manage.html?xxxId=${$(this).index()}&pageNum=1`;
    });

});





